module(..., package.seeall)

require "myUiWin"
require "aLiIotda"
require "tool"

-- PAGEID：窗口id
PAGEID = 0
local backColor = 0x0000 -- 背景色
local antiColor = 0xFFFF -- 选中反显色

local current_sub_table -- 保存当前层级的菜单内容

local table_index = {} -- 记录当前菜单在原始表的位置。如{1,2,3}：menu_table[1].content[1].content[2].content[3].content
local key_index = {}
local current_key = 1 -- 当前菜单table里面的一级菜单索引
local current_index = 1 -- 当前显示1-4行的选中位置
local show_first_key = 1 -- 当前显示菜单页面第一行 对应table里面的一级菜单索引key值
local page_title = ""

-- 要延迟一下，确保正确加载每个页面的PAGEID
sys.taskInit(function()
    sys.wait(2000)
    menu_table = {
        {
            title = "顶层",
            content = { -- 顶层固定，不能修改
                {
                    title = "1.添加子设备",
                    content = ui_device.PAGEID,
                    para = "device",
                    pic = "/lua/menu_net.png"
                }, {
                    title = "2.删除子设备",
                    content = ui_subset_del.PAGEID,
                    para = "adjust",
                    pic = "/lua/menu_adjust.png"
                }, {
                    title = "3.系统其他",
                    content = {
                        {
                            title = "1.重启设备",
                            content = ui_system.PAGEID,
                            para = "restart",
                            pic = "/lua/menu_restart.png"
                        }, {
                            title = "2.查询流量",
                            content = ui_sim.PAGEID,
                            para = "reset",
                            pic = "/lua/menu_reset.png"
                        }, {
                            title = "3.还原出厂设置",
                            content = ui_system.PAGEID,
                            para = "reset",
                            pic = "/lua/menu_reset.png"
                        }, {
                            title = "4.系统信息",
                            content = ui_system.PAGEID,
                            para = "info",
                            pic = "/lua/menu_info.png"
                        }, {
                            title = "5.出厂测试",
                            content = ui_test_factory.PAGEID,
                            para = "restart",
                            pic = "/lua/menu_restart.png"
                        }
                    },
                    para = "系统其他",
                    pic = "/lua/menu_others.png"
                }
            }
        },
        para = "选择菜单",
        pic = "/lua/menu_bind.png"
    }

    current_sub_table = tool.deepCopy(menu_table[1].content)

end)

local function show_menu()
    disp.clear()
    disp.setbkcolor(backColor)
    disp.setcolor(0xFFFF)
    disp.puttext(common.utf8ToGb2312(page_title), 10, 3)
    disp.drawrect(0, 22, 105, 23, 0x017E)
    disp.drawrect(110, 22, 160, 23, 0x06C0)

    -- 显示四行菜单标题
    for i = 1, 5 do
        if current_sub_table[show_first_key + i - 1] then
            if current_index == i then
                -- 改行被选中
                -- 先画选中方框底色
                disp.drawrect(0, 26 + (i - 1) * 20, 160, 44 + (i - 1) * 20,
                        antiColor)
                disp.setcolor(0x0)
                disp.puttext(common.utf8ToGb2312(
                        current_sub_table[show_first_key + i - 1].title),
                        4, (i - 1) * 20 + 28)
                if current_sub_table[show_first_key + i - 1].pic then
                    disp.putimage(current_sub_table[show_first_key + i - 1].pic,
                            125, 3)
                end
                if type(current_sub_table[show_first_key + i - 1].content) ==
                        "table" then
                    disp.putimage("/lua/narrow.png", 140, (i - 1) * 20 + 27)
                end
            else
                disp.setcolor(0xFFFF)
                disp.puttext(common.utf8ToGb2312(
                        current_sub_table[show_first_key + i - 1].title),
                        4, (i - 1) * 20 + 28)
                if type(current_sub_table[show_first_key + i - 1].content) ==
                        "table" then
                    disp.putimage("/lua/narrow_white.png", 140,
                            (i - 1) * 20 + 28)
                end
            end
        end
    end
    disp.update()
end

function initPage(para)
    if para == "index" then
        page_title = "选择菜单"
    else
        page_title = para
    end
    show_menu()
end

function refresh()
end

-- 根据table_index返回上一级菜单对应的table
local function findPreTable()
    local pre_table = tool.deepCopy(menu_table[1].content)
    -- 因为是上一级，所以只迭代到table里面的倒数第二个值
    for i = 1, #table_index - 1 do
        pre_table = pre_table[table_index[i]].content
        -- log.info("table_index", table_index[1])
    end
    return pre_table
end

function onKey(value)
    if value == "down" then
        if current_key == #current_sub_table then
            current_key = 0
        end
        current_key = current_key + 1 -- 按一下菜单对应索引增加1

        current_index = current_key % #current_sub_table -- 计算第几条要反显
        if current_index == 0 then
            current_index = #current_sub_table
        end

        show_first_key = (math.ceil(current_key / #current_sub_table) - 1) *
                #current_sub_table + 1 -- 计算显示第一条菜单是table里面的哪个索引位置
        show_menu()
        log.info("KEY NOW", #current_sub_table, current_key, current_index, show_first_key,
                page_title)
    elseif value == "enter" then
        -- 如果菜单content是数字的话，就要跳转页面
        if type(current_sub_table[current_key].content) == "number" then
            myUiWin.openWin(current_sub_table[current_key].content,
                    current_sub_table[current_key].para)

        else
            table.insert(table_index, current_key) -- 记录当前table的索引位置
            table.insert(key_index, {
                current_key, current_index, show_first_key, page_title
            })

            page_title = current_sub_table[current_key].para
            current_sub_table = current_sub_table[current_key].content -- 进入到下一层菜单，赋值到当前层级的菜单table

            log.info("KEYINDEX", #current_sub_table, current_key, current_index, show_first_key,
                    page_title)
            current_key = 1
            current_index = 1
            show_first_key = 1
            show_menu()
        end
    elseif value == "return" then
        if #table_index == 0 then
            -- 如果是顶层，返回主页
            myUiWin.openWin(ui_index.PAGEID, 0)
        else
            -- 返回上一层菜单
            current_sub_table = findPreTable()
            if current_sub_table[1].para == "menu" then
                page_title = "选择菜单"
            end
            table.remove(table_index) -- 要删除当前层级的索引位置记录
            current_key = key_index[#key_index][1]
            current_index = key_index[#key_index][2]
            show_first_key = key_index[#key_index][3]
            page_title = key_index[#key_index][4]
            log.info("RETURN KEYINDEX", current_key, current_index,
                    show_first_key, page_title)
            table.remove(key_index)
            show_menu()
        end
    elseif value == "return_long" then
        -- 强制返回主页
        current_sub_table = tool.deepCopy(menu_table[1].content)
        current_key = 1
        current_index = 1
        show_first_key = 1
        table_index = {}
        myUiWin.openWin(ui_index.PAGEID, 0)
    end
end
